package cn.hu.pet.controller;

import cn.hu.pet.service.IPetTypeService;
import cn.hu.pet.domain.PetType;
import cn.hu.pet.query.PetTypeQuery;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petType")
public class PetTypeController {
    @Autowired
    public IPetTypeService petTypeService;


    /**
     * 保存和修改公用的
     * @param petType 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody PetType petType) {
        if ( petType.getId() != null){
            petTypeService.update(petType);
        }else{
            petTypeService.add(petType);
        }
        return AjaxResult.getInstance();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        petTypeService.delete(id);
        return AjaxResult.getInstance();
    }

    //获取用户
    @GetMapping("/{id}")
    public PetType get(@PathVariable("id") Long id) {
        return petTypeService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<PetType> list() {
        return petTypeService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<PetType> json(@RequestBody PetTypeQuery query) {
        return petTypeService.listByQuery(query);
    }
}
